#!/usr/bin/env python
# -*- coding: utf-8 -*-
# datetime: 14/11/2023 下午9:49
# Author: name
# File: run.py
# ide: PyCharm
# description:
import os
import time

import pytest
import uvicorn
from fastapi import FastAPI
from pywebio import start_server
from pywebio.output import put_button
from starlette.background import BackgroundTasks

app = FastAPI()
bt = BackgroundTasks()


def run_pytest(report_id):
    print("我被加入后台任务，我将会在后台被执行")
    allure_path = f'./allures/{report_id}/temp'
    # pytest.main(['-vs', f'--alluredir={allure_path}', '--clean-alluredir', '--cache-clear'])
    pytest.main(['./test.py','-vs', f'--alluredir={allure_path}'])
    os.system(f"allure generate {allure_path} -o ./allures/{report_id}/report")
    # pin_update("show_data", value=f"执行完成，测试报告地址：http://{get_ip_address()}:8055/reports/{report_id}/report")


# @app.get("/run_test")
# async def run_pytest():
#     bt.add_task(run_pytest)
#     return "运行中请稍后"
def add_bt():
    report_id = int(time.time() * 1000)
    bt.add_task(run_pytest(report_id))

def show():
    put_button("执行", onclick=lambda: add_bt())


if __name__ == '__main__':
    start_server(show,port=8000)
    # uvicorn.run(app, )
